package com.android.often.util;

import java.io.Closeable;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import android.app.Activity;
import android.content.Context;

public class FileProperties {

	private Properties properties;
	private Activity activity;
	private String fileName;

	public FileProperties(Activity a, String fileName) {
		if (null == fileName)
			throw new IllegalArgumentException("fileName  must not null");
		properties = new Properties();
		activity = a;
		this.fileName = fileName;
		try {
			properties.load(a.openFileInput(fileName));
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}
	}

	public void put(String key, Object value) {
		properties.put(key, String.valueOf(value));
	}

	public void put(Map<Object, Object> map) {
		Set<Object> set = map.keySet();
		for (Object o : set) {
			Object value = map.get(o);
			put(o, value);
		}
	}

	private void put(Object key, Object value) {
		properties.put(String.valueOf(key), String.valueOf(value));
	}

	public void save(Object key, Object value) {
		put(key,value);
		save();
	}

	public boolean save(Map<Object, Object> value) {
		OutputStream out = null;
		boolean result = false;
		try {
			out = activity.openFileOutput(fileName,
					Context.MODE_WORLD_WRITEABLE);
			put(value);
			result = save(out);
		} catch (Exception e) {
		} finally {
			close(out);
		}
		return result;
	}

	private boolean save(OutputStream out) {
		try {
			properties.store(out, null);
		} catch (IOException e) {
		}
		return true;
	}

	public void save() {
		OutputStream out = null;
		try {
			out = activity.openFileOutput(fileName,
					Context.MODE_WORLD_WRITEABLE);
			save(out);
		} catch (FileNotFoundException e) {
		} finally {
			close(out);
		}
	}

	public synchronized float getFloat(String key, float defaultValue) {
		String o = (String) properties.get(key);
		if (null != o) {
			try {
				return Float.parseFloat(o);
			} catch (NumberFormatException e) {
			}
		}
		put(key, defaultValue);
		return defaultValue;
	}

	public synchronized String getString(String key, String defaultValue) {
		String o = properties.getProperty(key);

		if (null == o) {
			put(key, defaultValue);
			return defaultValue;
		}
		return o;
	}

	public synchronized boolean getBoolean(String key, boolean defaultValue) {
		String o = properties.getProperty(key);
		if (null == o) {
			put(key, defaultValue);
			return defaultValue;
		}
		return Boolean.parseBoolean(o);
	}

	public synchronized int getInt(String key, int defaultValue) {
		String o = properties.getProperty(key);

		if (null != o)
			try {
				return Integer.parseInt(o);
			} catch (NumberFormatException e) {
			}
		put(key, defaultValue);
		return defaultValue;
	}

	public long getLong(String key, long defaultValue) {
		String o = properties.getProperty(key);

		if (null != o) {
			try {
				return Long.parseLong(o);
			} catch (NumberFormatException e) {
			}
		}
		put(key, defaultValue);
		return defaultValue;
	}

	public Object get(Object o) {
		return properties.get(o);
	}

	private void close(Closeable stream) {
		if (null != stream)
			try {
				stream.close();
			} catch (IOException e) {
			}
		stream = null;
	}
}